package com.cwt_taxi_xa.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.cwt_taxi_xa.ActivityACC;
import com.cwt_taxi_xa.ActivityPower;
import com.cwt_taxi_xa.ActivitySPTest;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.data.DataUtil;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.can.manager.ViewManager;
import com.cwt_taxi_xa.receiver.SerialDataReceived;
import com.cwt_taxi_xa.record.RecordTimer;
import com.cwt_taxi_xa.record.RecorderBiz;
import com.cwt_taxi_xa.record.SystemRecord;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;
import com.cwt_taxi_xa.util.FileOper;
import com.cwt_taxi_xa.util.StringUtil;
import com.cwt_taxi_xa.util.Tools;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;
import java.util.Timer;

import android_serialport_api.SerialPort;

public class SerialPortService extends Service {
	String TAG = "SerialPortService";
	protected SerialPort mSerialPort1;
	protected OutputStream mOutputStream1;
	private InputStream mInputStream1;
	private ReadThread1 mReadThread1;

	protected SerialPort mSerialPort2;
	protected OutputStream mOutputStream2;
	private InputStream mInputStream2;
	private ReadThread2 mReadThread2;

//	protected SerialPort mSerialPort5;
//	protected OutputStream mOutputStream5;
//	private InputStream mInputStream5;
//	private ReadThread5 mReadThread5;

	protected SerialPort mSerialPort6;
	protected OutputStream mOutputStream6;
	private InputStream mInputStream6;
	private ReadThread6 mReadThread6;

	protected SerialPort mSerialPort7;
	protected OutputStream mOutputStream7;
	private InputStream mInputStream7;
	private ReadThread7 mReadThread7;

	public static SerialPortService instance;

	// 报警检测线程
	public static boolean IOThreadIsRun = false;
	// ACC检测线程
	public static boolean ACCThreadIsRun = false;
	// other检测线程
	public static boolean OtherThreadIsRun = false;

	public static Handler handler ;






















	public static SerialPortService getInstance() {
		return instance;
	}

	/**
	 * 初始化串口
	 *
	 * @return
	 * @throws IOException
	 * @throws InvalidParameterException
	 */
	public SerialPort getSerialPort1() throws SecurityException, IOException,
			InvalidParameterException {
		if (mSerialPort1 == null) {
			/* Read serial port parameters */
			String path = "/dev/ttyS1";
//			String path = "/dev/ttyS5";//gps原始数据
			if (!StringUtil.isNull(path)) {
//				int baudrate = 115200;
				int baudrate = 9600;
				/* Check parameters */
				if ((path.length() == 0) || (baudrate == -1)) {
					throw new InvalidParameterException();
				}
				// AppLog.i(TAG,
				// "串口测试：deviceType:"+Constants.deviceType+";串口path:"+path);
				/* Open the serial port */
				mSerialPort1 = new SerialPort(new File(path), baudrate, 0);
			}
		}
		return mSerialPort1;
	}

	public SerialPort getSerialPort2() throws SecurityException, IOException,
			InvalidParameterException {
		if (mSerialPort2 == null) {
			/* Read serial port parameters */
			String path = "/dev/ttyS2";
			if (!StringUtil.isNull(path)) {
				int baudrate = 9600;
				/* Check parameters */
				if ((path.length() == 0) || (baudrate == -1)) {
					throw new InvalidParameterException();
				}
				// AppLog.i(TAG,
				// "串口测试：deviceType:"+Constants.deviceType+";串口path:"+path);
				/* Open the serial port */
				mSerialPort2 = new SerialPort(new File(path), baudrate, 0);
			}
		}
		return mSerialPort2;
	}

//	public SerialPort getSerialPort5() throws SecurityException, IOException,
//	InvalidParameterException {
//		if (mSerialPort5 == null) {
//			/* Read serial port parameters */
//			String path = "/dev/ttyS5";
//			if (!StringUtil.isNull(path)) {
//				int baudrate = 9600;
//				/* Check parameters */
//				if ((path.length() == 0) || (baudrate == -1)) {
//					throw new InvalidParameterException();
//				}
//				// AppLog.i(TAG,
//				// "串口测试：deviceType:"+Constants.deviceType+";串口path:"+path);
//				/* Open the serial port */
//				mSerialPort5 = new SerialPort(new File(path), baudrate, 0);
//			}
//		}
//		return mSerialPort5;
//	}

	public SerialPort getSerialPort6() throws SecurityException, IOException,
			InvalidParameterException {
		if (mSerialPort6 == null) {
			/* Read serial port parameters */
			String path = "/dev/ttyS6";
			if (!StringUtil.isNull(path)) {
				int baudrate = 9600;//9600
				/* Check parameters */
				if ((path.length() == 0) || (baudrate == -1)) {
					throw new InvalidParameterException();
				}
				// AppLog.i(TAG,
				// "串口测试：deviceType:"+Constants.deviceType+";串口path:"+path);
				/* Open the serial port */
				mSerialPort6 = new SerialPort(new File(path), baudrate, 0);
			}
		}
		return mSerialPort6;
	}

	public SerialPort getSerialPort7() throws SecurityException, IOException,
			InvalidParameterException {
		if (mSerialPort7 == null) {
			/* Read serial port parameters */
			String path = "/dev/ttyS7";
			if (!StringUtil.isNull(path)) {
//				int baudrate = 9600;
				int baudrate = 115200;
				/* Check parameters */
				if ((path.length() == 0) || (baudrate == -1)) {
					throw new InvalidParameterException();
				}
				// AppLog.i(TAG,
				// "串口测试：deviceType:"+Constants.deviceType+";串口path:"+path);
				/* Open the serial port */
				mSerialPort7 = new SerialPort(new File(path), baudrate, 0);
			}
		}
		return mSerialPort7;
	}

	public synchronized void sendSerialData1(byte[] data) {
		try {
			getSerialPort1().getOutputStream().write(data);
//			Log.e("CarCallApplication", "发出串口数据-顶灯");
//			Log.e("数据", Tools.parseByte2HexStr(data));
//			AppLog.saveTxt("安卓发往串口一："+Tools.parseByte2HexStr(data));
//			AppLog.e("验证频繁空重车："+Tools.parseByte2HexStr(data));
			AppLog.savelog("安卓发往顶灯（串口1）："+Tools.parseByte2HexStr2(data));
			AppLog.e("安卓发往顶灯（串口1）："+Tools.parseByte2HexStr2(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized void sendSerialData2(byte[] data) {
		try {
			getSerialPort2().getOutputStream().write(data);
			Log.e("CarCallApplication", "发出串口数据-刷卡器");
			Log.e("数据", Tools.parseByte2HexStr(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	public synchronized void sendSerialData5(byte[] data) {
//		try {
//			getSerialPort5().getOutputStream().write(data);
////			Log.e("CarCallApplication", "发出串口数据-GPS模块");
////			Log.e("数据", Tools.parseByte2HexStr(data));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	public synchronized void sendSerialData6(byte[] data) {
		try {
			getSerialPort6().getOutputStream().write(data);
			Log.e(TAG, "发出串口数据-计价器 ：" + Tools.parseByte2HexStr(data));//+ Tools.parseByte2HexStr(data)
//			Log.e("数据", Tools.parseByte2HexStr(data));
			AppLog.savelog("发送计价器数据："+Tools.parseByte2HexStr(data));
//			AppLog.saveTxt_s6("串口6-发出:"+Tools.parseByte2HexStr(data));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized void sendSerialData7(byte[] data) {
		try {

			if(!ActivityACC.accon) return;

			if(
					(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5547"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5533"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5531"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5535"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5532"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("553E"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5545"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("554F"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5558"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("553F"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("556B"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("55F5"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5569"))||
							(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("5561"))
			){

//				if(!(Tools.parseByte2HexStr(Tools.byteTobyte(data, 0, 2)).equals("553E"))){
//					AppLog.e("发出MCU数据:"+Tools.parseByte2HexStr(data));
//				}
				getSerialPort7().getOutputStream().write(data);
				getSerialPort7().getOutputStream().flush();
				//调声耕注释
				//AppLog.savelog("发出MCU指令： "+Tools.parseByte2HexStr(data));
				//AppLog.e("MCU"+AppLog.generateTag());
				Log.d(TAG,"发出MCU指令： "+Tools.parseByte2HexStr(data));

			}

//			if(data.length<64){
//				getSerialPort7().getOutputStream().write(data);
//				getSerialPort7().getOutputStream().flush();

//			}else{
//				for (int i = 0; i < data.length/64; i++) {
//					getSerialPort7().getOutputStream().write(data, i*64, 64);
//				}
//				if(data.length%64!=0){
//					getSerialPort7().getOutputStream().write(data, data.length-(data.length%64), data.length%64);
//				}
//			}
//				Log.i("CarCallApplication", "发出MCU数据:"+Tools.parseByte2HexStr(data));
//			Log.e("数据", Tools.parseByte2HexStr(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void closeSerialPort() {
		if (mSerialPort1 != null) {
			mSerialPort1.close();
			mSerialPort1 = null;
		}
		if (mSerialPort2 != null) {
			mSerialPort2.close();
			mSerialPort2 = null;
		}
		if (mSerialPort6 != null) {
			mSerialPort6.close();
			mSerialPort6 = null;
		}
		if (mSerialPort7 != null) {
			mSerialPort7.close();
			mSerialPort7 = null;
		}
	}

	private class ReadThread1 extends Thread {

		@Override
		public void run() {

			super.run();
			byte[] buffer = new byte[64];
			int size;
			while (!isInterrupted()) {
				try {
//					buffer = new byte[64];
					if (mInputStream1 == null)
						return;
					size = mInputStream1.read(buffer);
					if (size > 0) {
						onDataReceived(1, buffer, size, SerialPortService.this);
					}
				} catch (IOException e) {
					AppLog.e(ExceptionUtil.getInfo(e));
					e.printStackTrace();
					return;
				}
			}
		}
	}

	private class ReadThread2 extends Thread {

		@Override
		public void run() {

			super.run();
			byte[] buffer = new byte[64];
			int size;
			while (!isInterrupted()) {
				try {
//					buffer = new byte[64];
					if (mInputStream2 == null)
						return;
					size = mInputStream2.read(buffer);
					if (size > 0) {
						onDataReceived(2, buffer, size, SerialPortService.this);
					}
				} catch (IOException e) {
					AppLog.e(ExceptionUtil.getInfo(e));
					e.printStackTrace();
					return;
				}
			}
		}
	}

//	private class ReadThread5 extends Thread {
//
//		@Override
//		public void run() {
//
//			super.run();
//			byte[] buffer = new byte[64];
//			int size;
//			while (!isInterrupted()) {
//				try {
////					buffer = new byte[64];
//					if (mInputStream5 == null)
//						return;
//					size = mInputStream5.read(buffer);
//					if (size > 0) {
//						onDataReceived(5, buffer, size, SerialPortService.this);
//					}
//				} catch (IOException e) {
//					AppLog.e(ExceptionUtil.getInfo(e));
//					e.printStackTrace();
//					return;
//				}
//			}
//		}
//	}

	private class ReadThread6 extends Thread {

		@Override
		public void run() {

			super.run();
			byte[] buffer = new byte[64];
			int size;
			while (!isInterrupted()) {
				try {
//				    buffer = new byte[64];
					if (mInputStream6 == null)
						return;
					size = mInputStream6.read(buffer);
					//AppLog.d(TAG,"ReadThread6 size:" + size);
					if (size > 0) {
						onDataReceived(6, buffer, size, SerialPortService.this);
					}
				} catch (IOException e) {
					AppLog.e(ExceptionUtil.getInfo(e));
					e.printStackTrace();
					return;
				}
			}
		}
	}

	private class ReadThread7 extends Thread {

		@Override
		public void run() {

			super.run();
			byte[] buffer = new byte[64];
			int size;
			while (!isInterrupted()) {
				try {
//					buffer = new byte[64];
					if (mInputStream7 == null)
						return;
					size = mInputStream7.read(buffer);
					if (size > 0) {
						onDataReceived(7, buffer, size, SerialPortService.this);
					}
				} catch (IOException e) {
					AppLog.e(ExceptionUtil.getInfo(e));
					e.printStackTrace();
					return;
				}
			}
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		AppLog.i("启动串口服务");
		//先初始化界面和ConditioningDecode,VehicleInformation中的Handler
		ViewManager.getInstance(this);
		MCUUtil.mConditioningDecode.init();
		MCUUtil.mVehicleInformation.init();

		createHandler();

		// 启动串口
		try {
			mSerialPort1 = getSerialPort1();
			mOutputStream1 = mSerialPort1.getOutputStream();
			mInputStream1 = mSerialPort1.getInputStream();
			mReadThread1 = new ReadThread1();
			mReadThread1.start();

			mSerialPort2 = getSerialPort2();
			mOutputStream2 = mSerialPort2.getOutputStream();
			mInputStream2 = mSerialPort2.getInputStream();
			mReadThread2 = new ReadThread2();
			mReadThread2.start();

//			mSerialPort5 = getSerialPort5();
//			mOutputStream5 = mSerialPort5.getOutputStream();
//			mInputStream5 = mSerialPort5.getInputStream();
//			mReadThread5 = new ReadThread5();
//			mReadThread5.start();

			mSerialPort6 = getSerialPort6();
			mOutputStream6 = mSerialPort6.getOutputStream();
			mInputStream6 = mSerialPort6.getInputStream();
			mReadThread6 = new ReadThread6();
			mReadThread6.start();

			mSerialPort7 = getSerialPort7();
			mOutputStream7 = mSerialPort7.getOutputStream();
			mInputStream7 = mSerialPort7.getInputStream();
			mReadThread7 = new ReadThread7();
			mReadThread7.start();

			handler.obtainMessage(3331).sendToTarget();
		} catch (IOException e) {
			AppLog.e(ExceptionUtil.getInfo(e));
			e.printStackTrace();
		}

		instance = this;


		//MCUUtil.hasVic(false);
		//MCUUtil.setvol(1, "串口服务启动后默认音量1");

		ViewManager.getInstance(this).showFloatBall();
		ViewManager.getInstance(this).showFuelAlarm();
		// 设置读卡器---弃用老版刷卡器
//		CardUtil.setAutoRead(1);

//		sendSerialData2(Tools.parseHexStr2Byte("55AA0004000021012455AA"));
//		sendSerialData2(Tools.parseHexStr2Byte("55AA0004032100011355AA"));


		// 启动报警按钮检测
//		if (!IOThreadIsRun) {
//			IOThreadIsRun = true;
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					try {
//						// FileReader localFileReader = new
//						// FileReader("/proc/detect_warning");
//						// BufferedReader localBufferedReader = new
//						// BufferedReader(localFileReader);
//						int sl = 0;
//						boolean baojing = true;
//						while (IOThreadIsRun) {
//							if (!ActivityACC.accon) {
//								try {
//									Thread.sleep(5000);
//								} catch (Exception e) {
//								}
//								continue;
//							}
//							try {
//								if (GPIOUtil
//										.readIOState("/proc/detect_warning") == 0) {
//									// if
//									// (localBufferedReader.readLine().equals("0"))
//									// {
//									if (sl > 5) {
//										sl = 0;
//										if (baojing) {
//											SerialPortService
//											.getInstance()
//											.sendSerialData1(
//													DDUtil.getDDstatue(6));
//											ConstantsAram.setAlarmStateValue(0,
//													0);
//											ConstantsAram.setAlarmStateValue(1,
//													0);
//											baojing = false;
//											RecordTimer.recordReason = 0;
//											// 停止录音
//											if(RecorderBiz.getRecordType() == 2){
//												if(MyStatus.zhongche){
//													// 重车,继续录音
//													RecordTimer.mediaIdList = new ArrayList<String>();
//												}else{
//													// 空车，结束录音
//													SerialPortService.handler.obtainMessage(1008).sendToTarget();
//												}
//											}
//										} else {
//											SerialPortService
//											.getInstance()
//											.sendSerialData1(
//													DDUtil.getDDstatue(7));
//											ConstantsAram.setAlarmStateValue(0,
//													1);
//											ConstantsAram.setAlarmStateValue(1,
//													1);
//											baojing = true;
//											TimerHeartBeat.baojingfasong = true;
//											// 录音
//											RecordTimer.recordReason = 2;
//											RecordTimer.mediaIdList = null;
//											if(RecorderBiz.getRecordType() == 2){
//												SerialPortService.handler.obtainMessage(1005).sendToTarget();
//											}else{
//												RecorderBiz recorderBiz = new RecorderBiz(MainApplication.getInstance());
//												recorderBiz.updateReason(RecordTimer.recordReason);
//											}
//										}
//									} else {
//										sl++;
//									}
//								} else {
//									sl = 0;
//								}
//
//								Thread.sleep(500);
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//				// });
//			}).start();
//		}

		// 启动ACC检测
//		if (!ACCThreadIsRun) {
//			ACCThreadIsRun = true;
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					try {
//						int acc = 0;
//						while (ACCThreadIsRun) {
//							try {
//								if (GPIOUtil.readIOState("/proc/acc_detect") == (ActivityACC.accon ? 1
//										: 0)) {
//									if (acc > 5) {
//										acc = 0;
//										handler_acc.sendEmptyMessage(10086);
//									} else {
//										acc++;
//									}
//								} else {
//									acc = 0;
//								}
//
//								Thread.sleep(1000);
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}).start();
//		}

		// 启动其他IO检测
//		if (!OtherThreadIsRun) {
//			OtherThreadIsRun = true;
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					try {
//						while (OtherThreadIsRun) {
//							try {
//								AppLog.e("/proc/input1------"+GPIOUtil.readIOState("/proc/input1"));
//								AppLog.e("/proc/input2------"+GPIOUtil.readIOState("/proc/input2"));
//								AppLog.e("/proc/input3------"+GPIOUtil.readIOState("/proc/input3"));
//								AppLog.e("/proc/input5------"+GPIOUtil.readIOState("/proc/input5"));
//								AppLog.e("/proc/input6------"+GPIOUtil.readIOState("/proc/input6"));
//								Thread.sleep(2000);
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			}).start();
//		}

	}

	private void createHandler() {
		// TODO Auto-generated method stub
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 1004){
					// 企标版本许需要录音，先屏蔽该功能 2014-09-25
					//启动系统录音功能
					try {
						if(RecorderBiz.getRecordType() == 1){
							if(Constants.recordTimer != null){
								Constants.recordTimer.cancel();
							}
							Constants.recordTimer = new Timer();
							RecordTimer recordTimer = new RecordTimer();
							Constants.recordTimer.schedule(recordTimer, 15*1000, RecorderBiz.getMaxTime()*60*1000);
							//						Constants.recordTimer.schedule(recordTimer, 15*1000, 2*60*1000);
						}
					} catch (Exception e) {
						AppLog.e(ExceptionUtil.getInfo(e), e);
						e.printStackTrace();
					}
				}else if(msg.what == 1005){
					AppLog.e("-----------------------------------1005");
					// 开启全程录音
					if(Constants.recordTimer != null){
						Constants.recordTimer.cancel();
					}
					Constants.recordTimer = new Timer();
					RecordTimer recordTimer = new RecordTimer();
					Constants.recordTimer.schedule(recordTimer, 1000, RecorderBiz.getMaxTime()*60*1000);
				}else if(msg.what == 1006){
					// 关闭全程录音
					SystemRecord.stop(true);
					if(Constants.recordTimer != null){
						Constants.recordTimer.cancel();
					}
				}else if(msg.what == 1007){
					// 如果是投诉，更改为投诉录音
					int evaluation = (Integer) msg.obj;
					//AppLog.v(TAG, "录音：结束录音，评价，isEnd："+isEnd+"; evaluation:"+evaluation);
					try {
						if(evaluation == 4){
							RecorderBiz recorderBiz = new RecorderBiz(MainApplication.getInstance());
							recorderBiz.updateReason(RecordTimer.mediaIdList);
						}
						RecordTimer.mediaIdList = null;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(RecorderBiz.getRecordType() == 2 && RecordTimer.recordReason != 2){
						// 评价后15秒关闭录音
						postDelayed(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								SystemRecord.stop(true);
								if(Constants.recordTimer != null){
									Constants.recordTimer.cancel();
								}
							}
						}, 15000);
					}
				}else if(msg.what == 1008){
					// 解除报警后关闭录音
					SystemRecord.stop(true);
					if(Constants.recordTimer != null){
						Constants.recordTimer.cancel();
					}
				}else if(msg.what == 2001){
					int text = (Integer) msg.obj;
				}else if(msg.what == 3331){
					// 检测录音文件是否大于500M
					final File file2 = new File(Constants.RECORDING_PATH);
					if(file2.exists()){
						long size = FileOper.getFileListSize(file2)/ 1024 / 1024;
						AppLog.i("录音，文件夹大小："+size);
						if(size > Constants.recordMaxSize){
							new Thread(){
								public void run() {
									// 大于499M，则删除至低于499M
									RecorderBiz recorderBiz = new RecorderBiz(SerialPortService.this);
									recorderBiz.deletefullInfos();
								};
							}.start();
						}
					}
				}
			}
		};
	}

	public void onDataReceived(int SPindex, final byte[] buffer,
							   final int size, Context context) {


		if(ActivitySPTest.isView&&SPindex==ActivitySPTest.SPindex){
			try {
				byte[] test = new byte[size];
				System.arraycopy(buffer, 0, test, 0, size);
				MainApplication.topActivity.sendMsg(1017, test);
			} catch (Exception e) {
			}
		}

		switch (SPindex) {
			case 1:
				if (!ActivityACC.accon) {
					return;
				}
				SerialDataReceived.onDataReceived1(buffer, size, context);
				break;
			case 2:
				if (!ActivityACC.accon) {
					return;
				}
				SerialDataReceived.onDataReceived2(buffer, size, context);
				break;
			case 5:
				if(ActivityPower.isview){
					SerialDataReceived.onDataReceived5(buffer, size, context);
				}
				break;
			case 6:
//			AppLog.saveTxt_s6("串口6-接收（缓存数据长度）:"+size);//----------------------------------------
				if (!ActivityACC.accon) {
					return;
				}
				if(DataUtil.getJJQ()==0){
					return;
				}
				SerialDataReceived.onDataReceived6(buffer, size, context);
				break;
			case 7:
				SerialDataReceived.onDataReceived7(buffer, size, context);
				break;
		}
	}

	@Override
	public void onDestroy() {
		if (mReadThread1 != null)
			mReadThread1.interrupt();
		if (mReadThread2 != null)
			mReadThread2.interrupt();
		if (mReadThread6 != null)
			mReadThread6.interrupt();
		if (mReadThread7 != null)
			mReadThread7.interrupt();
		closeSerialPort();
		mSerialPort1 = null;
		mSerialPort2 = null;
		mSerialPort6 = null;
		mSerialPort7 = null;
		super.onDestroy();
		// mHomeWatcher.stopWatch();// 在onPause中停止监听，不然会报错的。
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
